package net.realsee.service.wcs.warehouse.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.enums.wcs.charact.WcsCharactEnum;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.data.bo.WcsUnitCharactBo;
import net.realsee.domain.wcs.warehouse.WcsWarehouseLocation;
import net.realsee.domain.wcs.warehouse.bo.LocationFullInfoBo;
import net.realsee.domain.wcs.warehouse.bo.WcsWarehouseLocationBo;
import net.realsee.domain.wcs.warehouse.vo.WcsWarehouseLocationVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.gcl.domain.bo.ccs.GclCcsOperateResult;
import net.realsee.gcl.domain.bo.ccs.GclCcsRequestParams;
import net.realsee.mapper.wcs.warehouse.WcsWarehouseLocationMapper;
import net.realsee.service.wcs.warehouse.IWcsWarehouseLocationService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 库位Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsWarehouseLocationServiceImpl extends ServiceImpl<WcsWarehouseLocationMapper, WcsWarehouseLocation> implements IWcsWarehouseLocationService {

    private final WcsWarehouseLocationMapper baseMapper;

    /**
     * 查询库位
     */
    @Override
    public WcsWarehouseLocationVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询库位列表
     */
    @Override
    public TableDataInfo<WcsWarehouseLocationVo> selectVoPage(WcsWarehouseLocationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsWarehouseLocation> lqw = buildVoQueryWrapper(bo);
        Page<WcsWarehouseLocationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询库位列表
     */
    @Override
    public List<WcsWarehouseLocationVo> selectVoList(WcsWarehouseLocationBo bo) {
        LambdaQueryWrapper<WcsWarehouseLocation> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsWarehouseLocation> buildVoQueryWrapper(WcsWarehouseLocationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsWarehouseLocation> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsWarehouseLocation::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouse()), WcsWarehouseLocation::getWarehouse, bo.getWarehouse());
        lqw.eq(StringUtils.isNotBlank(bo.getLocation()), WcsWarehouseLocation::getLocation, bo.getLocation());
        lqw.like(StringUtils.isNotBlank(bo.getName()), WcsWarehouseLocation::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), WcsWarehouseLocation::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getRackNo()), WcsWarehouseLocation::getRackNo, bo.getRackNo());
        lqw.eq(bo.getXCoordinate() != null, WcsWarehouseLocation::getXCoordinate, bo.getXCoordinate());
        lqw.eq(bo.getYCoordinate() != null, WcsWarehouseLocation::getYCoordinate, bo.getYCoordinate());
        lqw.eq(bo.getZCoordinate() != null, WcsWarehouseLocation::getZCoordinate, bo.getZCoordinate());
        lqw.eq(StringUtils.isNotBlank(bo.getPosition()), WcsWarehouseLocation::getPosition, bo.getPosition());
        lqw.eq(bo.getStatus() != null, WcsWarehouseLocation::getStatus, bo.getStatus());
        lqw.eq(bo.getLocationType() != null, WcsWarehouseLocation::getLocationType, bo.getLocationType());
        lqw.eq(StringUtils.isNotBlank(bo.getMapMark()), WcsWarehouseLocation::getMapMark, bo.getMapMark());
        lqw.eq(StringUtils.isNotBlank(bo.getFixPositionRef()), WcsWarehouseLocation::getFixPositionRef, bo.getFixPositionRef());
        lqw.eq(StringUtils.isNotBlank(bo.getEqptLocation()), WcsWarehouseLocation::getEqptLocation, bo.getEqptLocation());
        lqw.eq(bo.getCtnrStatus() != null, WcsWarehouseLocation::getCtnrStatus, bo.getCtnrStatus());
        lqw.eq(bo.getCtnrAllowChange() != null, WcsWarehouseLocation::getCtnrAllowChange, bo.getCtnrAllowChange());
        lqw.between(params.get("beginCtnrUpdateOn") != null && params.get("endCtnrUpdateOn") != null,
            WcsWarehouseLocation::getCtnrUpdateOn, params.get("beginCtnrUpdateOn"), params.get("endCtnrUpdateOn"));
        lqw.eq(StringUtils.isNotBlank(bo.getCtnrUpdateBy()), WcsWarehouseLocation::getCtnrUpdateBy, bo.getCtnrUpdateBy());
        lqw.like(StringUtils.isNotBlank(bo.getTaskCode()), WcsWarehouseLocation::getTaskCode, bo.getTaskCode());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskStatus()), WcsWarehouseLocation::getTaskStatus, bo.getTaskStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskRcsStatus()), WcsWarehouseLocation::getTaskRcsStatus, bo.getTaskRcsStatus());
        lqw.between(params.get("beginTaskUpdateOn") != null && params.get("endTaskUpdateOn") != null,
            WcsWarehouseLocation::getTaskUpdateOn, params.get("beginTaskUpdateOn"), params.get("endTaskUpdateOn"));
        lqw.eq(StringUtils.isNotBlank(bo.getTaskUpdateBy()), WcsWarehouseLocation::getTaskUpdateBy, bo.getTaskUpdateBy());
        lqw.eq(bo.getX1Sen01Val() != null, WcsWarehouseLocation::getX1Sen01Val, bo.getX1Sen01Val());
        lqw.eq(bo.getX1Sen02Val() != null, WcsWarehouseLocation::getX1Sen02Val, bo.getX1Sen02Val());
        lqw.eq(bo.getY1Sen01Val() != null, WcsWarehouseLocation::getY1Sen01Val, bo.getY1Sen01Val());
        lqw.eq(bo.getY1Sen02Val() != null, WcsWarehouseLocation::getY1Sen02Val, bo.getY1Sen02Val());
        lqw.eq(StringUtils.isNotBlank(bo.getCcsPositionStatus()), WcsWarehouseLocation::getCcsPositionStatus, bo.getCcsPositionStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getWcsPositionStatus()), WcsWarehouseLocation::getWcsPositionStatus, bo.getWcsPositionStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsWarehouseLocation::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsWarehouseLocation::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsWarehouseLocation::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsWarehouseLocation::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsWarehouseLocation::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsWarehouseLocation::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsWarehouseLocation::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsWarehouseLocation::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsWarehouseLocation::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsWarehouseLocation::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsWarehouseLocation::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsWarehouseLocation::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsWarehouseLocation::getRemark, bo.getRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getZone()), WcsWarehouseLocation::getZone, bo.getZone());
        return lqw;
    }

    /**
     * 新增库位
     */
    @Override
    public Boolean insertByBo(WcsWarehouseLocationBo bo) {
        WcsWarehouseLocation add = BeanUtil.toBean(bo, WcsWarehouseLocation.class);
        return this.save(add);
    }

    /**
     * 修改库位
     */
    @Override
    public Boolean updateByBo(WcsWarehouseLocationBo bo) {
        WcsWarehouseLocation update = BeanUtil.toBean(bo, WcsWarehouseLocation.class);
        return this.updateById(update);
    }

    /**
     * 批量删除库位
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }

    /**
     * 通过库位查询库位信息
     */
    @Override
    public WcsWarehouseLocation getByLocation(String location) {
        return this.lambdaQuery().eq(WcsWarehouseLocation::getLocation, location).last("limit 1").one();
    }

    @Override
    public WcsWarehouseLocation getByMapMark(String mapMark) {
        return this.lambdaQuery().eq(WcsWarehouseLocation::getMapMark, mapMark).last("limit 1").one();
    }

    @Override
    public WcsWarehouseLocation getByWaitMapMark(String waitMapMark) {
        return this.lambdaQuery().eq(WcsWarehouseLocation::getWaitMapMark, waitMapMark).last("limit 1").one();
    }

    /**
     * 通过库位查询库位完整信息
     */
    @Override
    public LocationFullInfoBo getFullInfoByLocation(String location) {
        return this.baseMapper.getFullInfoByLocation(location);
    }

    /**
     * 根据库位编号更新库位状态
     *
     * @param location
     * @param status
     */
    @Override
    public void updateWarehouseStatus(String location, Integer status) {
        baseMapper.updateWarehouseStatus(location, status);
    }

    /**
     * 根据库位编号、任务号，查询该库位的任务状态
     *
     * @param requestParams
     * @return
     */
    @Override
    public GclCcsOperateResult queryWarehouseLocTaskStatus(GclCcsRequestParams requestParams) {
        return baseMapper.queryWarehouseLocTaskStatus(requestParams);
    }

    /**
     * 根据库位查询传感器距离
     *
     * @param location
     * @return
     */
    @Override
    public List<WcsUnitCharactBo> getSensorDistance(String location) {
        return baseMapper.getSensorDistance(location);
    }

    /**
     * 通过库位号和特性，查询特性值
     */
    @Override
    public String getValueByLocationAndCharact(String location, WcsCharactEnum wcsCharactEnum) {
        return baseMapper.getValueByLocationAndCharact(location, wcsCharactEnum.getCode());
    }
}
